/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.metadatacompile;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.bizentity.operation.BizMgrAction;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementDataType;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.designtime.api.element.GspEnumValue;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.action.ViewModelAction;

public
class CheckCoincidenceException {
  //BE Level Exceptions
  public static String throwObjectDataTypeException(String viewObjectCode) {
    StringBuffer strBuffer = new StringBuffer("\n\nErrorNum. ViewModel节点数据类型异常,请检查！");
    strBuffer.append(curViewModelAndObjectInfo(viewObjectCode));
    return strBuffer.toString();
  }

  //组合输出ViewModel和ViewModel节点信息
  private static String curViewModelAndObjectInfo(String viewObjectCode) {
    StringBuffer strBuffer = new StringBuffer();
    strBuffer.append("  ViewModel节点编号： ");
    strBuffer.append(viewObjectCode);
    return strBuffer.toString();
  }

  //Mgr动作异常信息
  public static String throwVOMgrActionException(GspViewModel viewModel, ViewModelAction action,
      GspBusinessEntity businessEntity) {
    //VO【XXX】上动作在对应BE上找不到。\n VO详情\n：VO编号：VO名称；动作编号： 动作名称：；对应BE编号： BE名称
    StringBuilder strBuilder = new StringBuilder("\n\nErrorNum. VO【");
    strBuilder.append(viewModel.getCode())
        .append("】上动作在对应BE上找不到。 ")
        .append(getViewModelMessage(viewModel))
        .append("动作编号:")
        .append(action.getCode())
        .append(getBusinessEntityMessage(businessEntity));
    return strBuilder.toString();
  }

  public static String throwVOMgrActionReturnValue(GspViewModel viewModel, ViewModelAction voAction,
      GspBusinessEntity businessEntity,
      BizMgrAction bizMgrAction) {
    //VO【XXX】上动作返回值类型不一致，VO动作返回值类型是【XXX】,对应BE动作返回值类型为【XXX】。VO详情\n：VO编号：VO名称；动作编号： 动作名称：；对应BE编号： BE名称
    StringBuilder strBuilder = new StringBuilder("\n\nErrorNum. VO【");
    strBuilder
        .append(viewModel.getCode())
        .append("】上动作返回值类型不一致，VO动作返回值类型是【")
        .append(voAction.getReturnValue().getClassName())
        .append("】,对应BE动作返回值类型为【")
        .append(bizMgrAction.getReturnValue().getClassName())
        .append("】。")
        .append(getViewModelMessage(viewModel))
        .append("动作编号:")
        .append(voAction.getCode())
        .append(getBusinessEntityMessage(businessEntity));
    return strBuilder.toString();
  }

  //节点异常信息
  public static String throwVmObjectMappingException(GspViewModel viewModel,
      IGspCommonObject viewObject) {
    //VO【XXX】上节点【XXX】Mapping失败。 VO详情\n：VO编号：VO名称；
    StringBuilder strBuilder = new StringBuilder("\n\nErrorNum. VO【");
    strBuilder
        .append(viewModel.getCode())
        .append("】上节点【")
        .append(viewObject.getCode())
        .append("】在BE上没有找到对应的节点。")
        .append(getViewModelMessage(viewModel))
        .append(getViewObjectMessage(viewObject));
    return strBuilder.toString();
  }


  public static String throwBizEntityNotFindException(GspViewModel viewModel,
      IGspCommonObject viewObject) {
    //VO【XXX】上节点【XXX】在对应BE上没有找到。 VO详情\n：VO编号：VO名称；
    StringBuilder strBuilder = new StringBuilder("\n\nErrorNum. VO【");
    strBuilder
        .append(viewModel.getCode())
        .append("】上节点【")
        .append(viewObject.getCode())
        .append("】在对应的BE上没有找到。")
        .append(getViewModelMessage(viewModel))
        .append(getViewObjectMessage(viewObject));
    return strBuilder.toString();
  }

  public static String throwVoNodeCodeNotEqualException(GspViewModel viewModel,
      IGspCommonObject viewObject, GspBusinessEntity bizEntity,
      GspBizEntityObject bizEntityObject) {
    //VO【XXX】上节点【XXX】的节点编号不一致。VO上的节点编号为【XXX】,对应BE节点编号为【XXX】。\nVO详情：VO编号：VO名称；VO节点详情：节点编号，节点名称；对应BE详情:BE编号，BE名称；BE节点详情：节点编号，节点名称；
    StringBuilder strBuilder = new StringBuilder("\n\nErrorNum. VO【");
    strBuilder
        .append(viewModel.getCode())
        .append("】上节点【")
        .append(viewObject.getCode())
        .append("】的节点编号不一致。VO上的节点编号为【")
        .append(viewObject.getCode())
        .append("】对应BE上节点编号为【")
        .append(bizEntity.getCode())
        .append("】")
        .append(getViewModelMessage(viewModel))
        .append(getViewObjectMessage(viewObject))
        .append(getBusinessEntityMessage(bizEntity))
        .append(getBizEntityObjectMessage(bizEntityObject));
    return strBuilder.toString();
  }

  //字段异常提示
  public static String throwElementException(GspViewModel viewModel,
      IGspCommonObject viewObject,IGspCommonField vmElement,String message){
    //VO【XXX】上节点【XXX】上字段【XXX】数据类型异常。\nVO详情：VO编号：VO名称；VO节点详情：节点编号，节点名称；字段编号，字段名称；
    StringBuilder strBuilder =  new StringBuilder(VOFieldRelatedMessage(viewModel,viewObject,vmElement));
    strBuilder
        .append(vmElement.getCode())
        .append(message)
        .append(getViewModelMessage(viewModel))
        .append(getViewObjectMessage(viewObject))
        .append(getCommonFieldMessage(vmElement));
    return strBuilder.toString();
  }

  public static String throwFieldLabelIDException(GspViewModel viewModel,
      IGspCommonObject viewObject,IGspCommonField vmElement,GspBusinessEntity businessEntity,GspBizEntityObject bizEntityObject,IGspCommonField beElement) {
    //VO【XXX】上节点【XXX】上字段【XXX】标签不一致。VO上字段标签【XXX】,对应BE上字段标签【XXX】\nVO详情：VO编号：VO名称；VO节点详情：节点编号，节点名称；字段编号，字段名称。\n对应BE详情：BE编号，BE名称；BE节点详情：节点编号，节点名称，字段名称，字段编号；
    StringBuilder strBuilder =  new StringBuilder(VOFieldRelatedMessage(viewModel,viewObject,vmElement));
    strBuilder.append("上字段标签不一致。VO上的字段标签【")
        .append(vmElement.getLabelID())
        .append("】对应BE字段标签为【")
        .append(beElement.getLabelID())
        .append("】")
        .append(getFieldRelatedMessage(viewModel,viewObject,vmElement,businessEntity,bizEntityObject,beElement));
    return strBuilder.toString();
  }

  public static String throwObjectTypeException(GspViewModel viewModel,
      IGspCommonObject viewObject,IGspCommonField vmElement,GspBusinessEntity businessEntity,GspBizEntityObject bizEntityObject,IGspCommonField beElement) {
    //VO【XXX】上节点【XXX】上字段【XXX】标签不一致。VO上字段数据类型为【XXX】,对应BE上字段数据类型【XXX】\nVO详情：VO编号：VO名称；VO节点详情：节点编号，节点名称；字段编号，字段名称。\n对应BE详情：BE编号，BE名称；BE节点详情：节点编号，节点名称，字段名称，字段编号；
    StringBuilder strBuilder =  new StringBuilder(VOFieldRelatedMessage(viewModel,viewObject,vmElement));
    strBuilder.append("类型不一致。VO上字段类型为【")
        .append(convertObjectTypeName(vmElement.getObjectType()))
        .append("】对应BE上字段的类型为【")
        .append(convertObjectTypeName(beElement.getObjectType()))
        .append("】")
        .append(getFieldRelatedMessage(viewModel,viewObject,vmElement,businessEntity,bizEntityObject,beElement));
    return strBuilder.toString();
  }

  public static String throwDataTypeException(GspViewModel viewModel,
      IGspCommonObject viewObject,IGspCommonField vmElement,GspBusinessEntity businessEntity,GspBizEntityObject bizEntityObject,IGspCommonField beElement) {
    //VO【XXX】上节点【XXX】上字段【XXX】标签不一致。VO上字段数据类型为【XXX】,对应BE上字段数据类型【XXX】\nVO详情：VO编号：VO名称；VO节点详情：节点编号，节点名称；字段编号，字段名称。\n对应BE详情：BE编号，BE名称；BE节点详情：节点编号，节点名称，字段名称，字段编号；
    StringBuilder strBuilder =  new StringBuilder(VOFieldRelatedMessage(viewModel,viewObject,vmElement));
        strBuilder.append("数据类型不一致。VO上字段数据类型为【")
        .append(convertMDataTypeName(vmElement.getMDataType()))
        .append("】对应BE上字段的数据类型为【")
        .append(convertMDataTypeName(beElement.getMDataType()))
        .append("】")
        .append(getFieldRelatedMessage(viewModel,viewObject,vmElement,businessEntity,bizEntityObject,beElement));
    return strBuilder.toString();
  }

  public static String throwFieldUdtException(GspViewModel viewModel,
      IGspCommonObject viewObject,IGspCommonField vmElement,GspBusinessEntity businessEntity,GspBizEntityObject bizEntityObject,IGspCommonField beElement){
    StringBuilder strBuilder =  new StringBuilder(VOFieldRelatedMessage(viewModel,viewObject,vmElement));
    if(vmElement.getIsUdt()){
      strBuilder.append("VO字段是UDT类型，对应BE上字段不是UDT类型");
    }else {
      strBuilder.append("VO字段是不是UDT类型，对应BE上字段是UDT类型");
    }
    strBuilder.append(getFieldRelatedMessage(viewModel,viewObject,vmElement,businessEntity,bizEntityObject,beElement));
    return strBuilder.toString();
  }

  public static String throwFieldAssociationException(GspViewModel viewModel,
      IGspCommonObject viewObject,IGspCommonField vmElement,GspBusinessEntity businessEntity,GspBizEntityObject bizEntityObject,IGspCommonField beElement,String message){
      StringBuilder strBuilder =  new StringBuilder(VOFieldRelatedMessage(viewModel,viewObject,vmElement));
      //strBuilder.append("关联字段对应的BE上字段不是关联类型。");
      strBuilder.append(message);
    strBuilder.append(getFieldRelatedMessage(viewModel,viewObject,vmElement,businessEntity,bizEntityObject,beElement));
    return strBuilder.toString();
  }


  public static String throwEnumValueException(GspViewModel viewModel,
      IGspCommonObject viewObject,IGspCommonField vmElement,GspBusinessEntity businessEntity,GspBizEntityObject bizEntityObject,IGspCommonField beElement,GspEnumValue vmItem, GspEnumValue beItem) {
    StringBuilder strBuilder = new StringBuilder(
        VOFieldRelatedMessage(viewModel, viewObject, vmElement));
    strBuilder.append("枚举字段定义不一致，请检查! 不一致序号为：")
    .append(vmItem.getIndex())
    .append("枚举值为:")
    .append(vmItem.getValue())
    .append("对应枚举值为：")
    .append(beItem.getValue());
    strBuilder.append(getFieldRelatedMessage(viewModel,viewObject,vmElement,businessEntity,bizEntityObject,beElement));
    return strBuilder.toString();
  }


  //工具方法
  private static String getViewModelMessage(GspViewModel viewModel) {
    StringBuilder strBuilder = new StringBuilder(" VO详情: VO编号：");
    strBuilder.append(viewModel.getCode());
    return strBuilder.toString();
  }


  private static String getBusinessEntityMessage(GspBusinessEntity businessEntity) {
    StringBuilder strBuilder = new StringBuilder(" 对应BE编号：");
    strBuilder.append(businessEntity.getCode());
    return strBuilder.toString();
  }

  private static String getViewObjectMessage(IGspCommonObject viewObject) {
    StringBuilder strBuffer = new StringBuilder(" VO节点详情： 节点编号：");
    strBuffer.append(viewObject.getCode());
    return strBuffer.toString();
  }

  private static String getBizEntityObjectMessage(GspBizEntityObject bizEntityObject) {
    StringBuilder strBuilder = new StringBuilder(" BE节点详情:  节点编号：");
    strBuilder.append(bizEntityObject.getCode());
    return strBuilder.toString();
  }
  private static String getCommonFieldMessage(IGspCommonField commonField){
    StringBuilder strBuilder = new StringBuilder(" 字段详情： 字段编号：" );
    strBuilder.append(commonField.getCode());
    return strBuilder.toString();
  }
  private  static String getFieldRelatedMessage(GspViewModel viewModel,
      IGspCommonObject viewObject,IGspCommonField vmElement,GspBusinessEntity businessEntity,GspBizEntityObject bizEntityObject,IGspCommonField beElement){
    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append(getViewModelMessage(viewModel))
        .append(getViewObjectMessage(viewObject))
        .append(getCommonFieldMessage(vmElement))
        .append(getBusinessEntityMessage(businessEntity))
        .append(getBizEntityObjectMessage(bizEntityObject))
        .append(getCommonFieldMessage(beElement));
    return strBuilder.toString();
  }

  private static String VOFieldRelatedMessage(GspViewModel viewModel, IGspCommonObject viewObject,IGspCommonField vmElement){
    StringBuilder strBuilder =  new StringBuilder("\n\nErrorNum. VO【");
    strBuilder
        .append(viewModel.getCode())
        .append("】上节点【")
        .append(viewObject.getCode())
        .append("】上字段【")
        .append(vmElement.getCode())
        .append("】");
    return strBuilder.toString();
  }
  private static String convertObjectTypeName(GspElementObjectType objectType){
    String typeName =  new String();
    switch (objectType){
      case None:
        typeName = "无";
        break;
      case Association:
        typeName = "关联";
        break;
      case Enum:
        typeName = "枚举";
        break;
      case DynamicProp:
        typeName = "动态属性";
        break;
      default:
        break;
    }
    return typeName;
  }
  private static String convertMDataTypeName(GspElementDataType dataType){
    String typeName = new String();
    switch (dataType){
      case String:
        typeName = "文本";
        break;
      case Text:
        typeName = "备注";
        break;
      case Integer:
        typeName = "整数";
        break;
      case Decimal:
        typeName = "浮点数字";
        break;
      case Boolean:
        typeName = "布尔型";
        break;
      case Date:
        typeName = "日期";
        break;
      case DateTime:
        typeName = "日期时间";
        break;
      case Binary:
        typeName = "二进制";
        break;
      default:
        break;
    }
    return typeName;
  }
}
